﻿' Developer Express Code Central Example:
' GridLookUpEdit: multiple selection using a checkbox (web style) with the predefined OK and Cancel buttons
' 
' The current example extends the functionality described in
' http://www.devexpress.com/scid=E3074 example.
' In the mentioned example,
' GridlookUpEdit.EditValue is changed immediately after a user has selected a row
' in a drop-down GridView instance.
' This example demonstrates how to implement
' the functionality to confirm/reject changing GridlookUpEdit.EditValue when a
' popup Form is closing. This functionality is implemented by adding two
' predefined buttons: Ok and Cancel.
' 
' In addition, this example illustrates how
' to use a different collection as a GridlookUpEdit.DataSource property value.
' 
' You can find sample updates and versions for different programming languages here:
' http://www.devexpress.com/example=E4048

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Registrator
Imports DevExpress.XtraEditors.ViewInfo
Imports DevExpress.XtraEditors.Drawing
Imports System.ComponentModel
Imports DevExpress.XtraEditors.Popup
Imports System.IO
Imports System.Collections
Imports System.Data
Imports System.Windows.Forms

Namespace IControls
    <UserRepositoryItem("RegisterRepositoryItemIGridLookUpEdit")> _
    Public Class RepositoryItemIGridLookUpEdit
        Inherits RepositoryItemGridLookUpEdit
        'The static constructor which calls the registration method
        Shared Sub New()
            RegisterMyRepositoryItemGridLookUpEdit()
        End Sub


        Friend gridSelection_ As GridCheckMarksSelection
        Public Property GridSelection() As GridCheckMarksSelection
            Get
                Return gridSelection_
            End Get
            Set(ByVal value As GridCheckMarksSelection)
                If gridSelection_ IsNot Nothing Then
                    RemoveHandler gridSelection_.SelectionChanged, AddressOf gridSelection__SelectionChanged
                End If
                gridSelection_ = value
                AddHandler gridSelection_.SelectionChanged, AddressOf gridSelection__SelectionChanged
            End Set

        End Property

        Private Sub gridSelection__SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)

            Dim sb As New StringBuilder()
            Dim collection As PropertyDescriptorCollection = ListBindingHelper.GetListItemProperties(GridSelection.Selection)
            Dim desc As PropertyDescriptor = collection(DisplayMember)
            For Each rv As Object In GridSelection.Selection
                If sb.ToString().Length > 0 Then
                    sb.Append(", ")
                End If
                sb.Append(desc.GetValue(rv).ToString())
            Next rv
            If OwnerEdit IsNot Nothing Then
                OwnerEdit.Text = sb.ToString()
            End If
        End Sub

        'Initialize new properties
        Public Sub New()
            GridSelection = New GridCheckMarksSelection()
        End Sub

        'The unique name for the custom editor
        Public Const CustomEditName As String = "IGridLookUpEdit"

        'Return the unique name
        Public Overrides ReadOnly Property EditorTypeName() As String
            Get
                Return CustomEditName
            End Get
        End Property

        'Register the editor
        Public Shared Sub RegisterMyRepositoryItemGridLookUpEdit()
            EditorRegistrationInfo.Default.Editors.Add(New EditorClassInfo(CustomEditName, GetType(IGridLookUpEdit), GetType(RepositoryItemIGridLookUpEdit), GetType(GridLookUpEditBaseViewInfo), New ButtonEditPainter(), True))
        End Sub

        Public Overrides Sub Assign(ByVal item As RepositoryItem)
            BeginUpdate()
            Try
                MyBase.Assign(item)
                Dim source As RepositoryItemIGridLookUpEdit = TryCast(item, RepositoryItemIGridLookUpEdit)
                If source Is Nothing Then
                    Return
                End If
                GridSelection = source.GridSelection
            Finally
                EndUpdate()
            End Try
        End Sub
    End Class

    Public Class IGridLookUpEdit
        Inherits GridLookUpEdit
        'The static constructor which calls the registration method
        Shared Sub New()
            RepositoryItemIGridLookUpEdit.RegisterMyRepositoryItemGridLookUpEdit()
        End Sub

        'Initialize the new instance
        Public Sub New()
            '...
            AddHandler CustomDisplayText, AddressOf IGridLookUpEdit_CustomDisplayText
        End Sub

        Private Sub IGridLookUpEdit_CustomDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs)
            Dim sb As New StringBuilder()
            Dim collection As PropertyDescriptorCollection = ListBindingHelper.GetListItemProperties(Properties.GridSelection.Selection)
            Dim desc As PropertyDescriptor = collection(Properties.DisplayMember)
            For Each rv As Object In Properties.GridSelection.Selection
                If sb.ToString().Length > 0 Then
                    sb.Append(", ")
                End If
                sb.Append(desc.GetValue(rv).ToString())
            Next rv
            e.DisplayText = sb.ToString()
        End Sub

        'Return the unique name
        Public Overrides ReadOnly Property EditorTypeName() As String
            Get
                Return RepositoryItemIGridLookUpEdit.CustomEditName
            End Get
        End Property

        'Override the Properties property
        'Simply type-cast the object to the custom repository item type
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Shadows ReadOnly Property Properties() As RepositoryItemIGridLookUpEdit
            Get
                Return TryCast(MyBase.Properties, RepositoryItemIGridLookUpEdit)
            End Get
        End Property

        Protected Overrides Function CreatePopupForm() As DevExpress.XtraEditors.Popup.PopupBaseForm
            Return New MyPopupGridLookUpEditForm(Me)
        End Function

        Protected Overrides Sub OnPopupShown()
            MyBase.OnPopupShown()
        End Sub
    End Class

    Public Class MyPopupGridLookUpEditForm
        Inherits PopupGridLookUpEditForm
        Public Sub New(ByVal edit As IGridLookUpEdit)
            MyBase.New(edit)
        End Sub

        Private tempSelection As New ArrayList()

        Protected Overrides Sub SetupButtons()
            Me.fShowOkButton = True
            Me.fCloseButtonStyle = BlobCloseButtonStyle.Caption
        End Sub

        Protected Overrides Sub OnCloseButtonClick()
            MyBase.OnCloseButtonClick()
            RedefineSelection(tempSelection, (TryCast(OwnerEdit, IGridLookUpEdit)).Properties.GridSelection.Selection)
        End Sub

        Protected Overrides Sub OnOkButtonClick()
            MyBase.OnOkButtonClick()
            TryCast(OwnerEdit, IGridLookUpEdit).Properties.GridSelection.OnSelectionChanged()
        End Sub

        Protected Overrides Sub OnBeforeShowPopup()
            MyBase.OnBeforeShowPopup()
            RedefineSelection((TryCast(OwnerEdit, IGridLookUpEdit)).Properties.GridSelection.Selection, tempSelection)
        End Sub

        Friend Sub RedefineSelection(ByVal listSource As ArrayList, ByVal listDestination As ArrayList)
            listDestination.Clear()
            For Each item In listSource
                listDestination.Add(item)
            Next item
        End Sub

        Protected Overrides Function CreateViewInfo() As PopupBaseFormViewInfo
            Return New MyCustomBlobPopupFormViewInfo(Me)
        End Function
    End Class

    Public Class MyCustomBlobPopupFormViewInfo
        Inherits CustomBlobPopupFormViewInfo
        Public Sub New(ByVal form As PopupBaseForm)
            MyBase.New(form)
        End Sub

        ' recalculate buttons sizes if needed
        Protected Overrides Sub CalcContentRect(ByVal bounds As System.Drawing.Rectangle)
            MyBase.CalcContentRect(bounds)
            ' recalculate buttons bounds if needed
            Me.fOkButtonRect = New System.Drawing.Rectangle(Me.fOkButtonRect.X, Me.fOkButtonRect.Y + 1, Me.fOkButtonRect.Width, Me.fOkButtonRect.Height - 2)
            Me.fCloseButtonRect = New System.Drawing.Rectangle(Me.fCloseButtonRect.X, Me.fCloseButtonRect.Y + 1, Me.fCloseButtonRect.Width, Me.fCloseButtonRect.Height - 2)

            ' recalculate footer bounds if needed
            Me.SizeBarRect = New System.Drawing.Rectangle(Me.SizeBarRect.X, Me.SizeBarRect.Y - 20, Me.SizeBarRect.Width, Me.SizeBarRect.Height + 20)
            Me.fContentRect = New System.Drawing.Rectangle(Me.fContentRect.X, Me.fContentRect.Y, Me.fContentRect.Width, Me.fContentRect.Height - 20)
            Me.fOkButtonRect = New System.Drawing.Rectangle(Me.fOkButtonRect.X, Me.fOkButtonRect.Y - 10, Me.fOkButtonRect.Width, Me.fOkButtonRect.Height)
            Me.fCloseButtonRect = New System.Drawing.Rectangle(Me.fCloseButtonRect.X, Me.fCloseButtonRect.Y - 10, Me.fCloseButtonRect.Width, Me.fCloseButtonRect.Height)
        End Sub
    End Class
End Namespace
